#include "json_to_data.h"
#include <string.h>
#include <rtthread.h>

int parse_uint8(const cJSON *obj_json, const char *key, unsigned char *value)
{
    const cJSON *item_json = cJSON_GetObjectItemCaseSensitive(obj_json, key);
    // check the type
    if (cJSON_IsNumber(item_json))
    {
        *value = item_json->valuedouble;
        return 0;
    }

    return 1;
}

int parse_uint16(const cJSON *obj_json, const char *key, unsigned short *value)
{
    const cJSON *item_json = cJSON_GetObjectItemCaseSensitive(obj_json, key);
    // check the type
    if (cJSON_IsNumber(item_json))
    {
        *value = item_json->valuedouble;
        return 0;
    }

    return 1;
}

int parse_uint32(const cJSON *obj_json, const char *key, unsigned int *value)
{
    const cJSON *item_json = cJSON_GetObjectItemCaseSensitive(obj_json, key);
    // check the type
    if (cJSON_IsNumber(item_json))
    {
        *value = item_json->valuedouble;
        return 0;
    }

    return 1;
}

int parse_uint8_array(const cJSON *obj_json, const char *key, unsigned char *array, unsigned char array_len)
{
    const cJSON *item_json = NULL;
    const cJSON *array_json = cJSON_GetObjectItemCaseSensitive(obj_json, key);
    unsigned char i = 0;
    // check the type
    if (!cJSON_IsArray(array_json))
    {
        return 1;
    }

    cJSON_ArrayForEach(item_json, array_json)
    {
        // check the type
        if (cJSON_IsNumber(item_json))
        {
            array[i] = item_json->valuedouble;
        }
        if (array_len == i++)
        {
            break;
        }
    }

    return 0;
}

int parse_uint16_array(const cJSON *obj_json, const char *key, unsigned short *array, unsigned char array_len)
{
    const cJSON *item_json = NULL;
    const cJSON *array_json = cJSON_GetObjectItemCaseSensitive(obj_json, key);
    unsigned char i = 0;
    // check the type
    if (!cJSON_IsArray(array_json))
    {
        return 1;
    }

    cJSON_ArrayForEach(item_json, array_json)
    {
        // check the type
        if (cJSON_IsNumber(item_json))
        {
            array[i] = item_json->valuedouble;
        }
        if (array_len == i++)
        {
            break;
        }
    }

    return 0;
}

int parse_uint32_array(const cJSON *obj_json, const char *key, unsigned int *array, unsigned char array_len)
{
    const cJSON *item_json = NULL;
    const cJSON *array_json = cJSON_GetObjectItemCaseSensitive(obj_json, key);
    unsigned char i = 0;
    // check the type
    if (!cJSON_IsArray(array_json))
    {
        return 1;
    }

    cJSON_ArrayForEach(item_json, array_json)
    {
        // check the type
        if (cJSON_IsNumber(item_json))
        {
            array[i] = item_json->valuedouble;
        }
        if (array_len == i++)
        {
            break;
        }
    }

    return 0;
}

int parse_string(const cJSON *obj_json, const char *key, char *str, char strlen)
{
    const cJSON *item = cJSON_GetObjectItemCaseSensitive(obj_json, key);
    // check the type
    if (cJSON_IsString(item))
    {
        rt_strncpy(str, item->valuestring, strlen);
        return 0;
    }
    return 1;
}
